package org.lib93.com.libSkin.view

import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.ViewCompat
import org.lib93.com.libSkin.utils.*

class SkinView (val view: View,val attributes: List<SkinAttribute>){
    fun apply(){
        applySkinSupport()
        if (attributes.isEmpty())
            return
        var left: Drawable? = null
        var top: Drawable? = null
        var right: Drawable? = null
        var bottom: Drawable? = null
        attributes.forEach {
            when(it.name){
                BACKGROUND -> {
                    val background = SkinResources.getInstance(view.context).getBackground(it.id)
                    if (background is Int)
                        view.setBackgroundColor(background)
                    else
                        view.background =(background as Drawable )
                     //   ViewCompat.setBackground(view, background as Drawable)
                }
                SRC -> {
                    val src = SkinResources.getInstance(view.context).getBackground(it.id)
                    if (src is Int)
                        (view as ImageView).setImageDrawable(ColorDrawable(src))
                    else
                        (view as ImageView).setImageDrawable(src as Drawable)
                }
                TEXTCOLOR -> {
                    (view as TextView).setTextColor(SkinResources.getInstance(view.context).getColor(it.id))
                }
                DRAWABLELEFT -> {
                    left = SkinResources.getInstance(view.context).getDrawable(it.id)
                }
                DRAWABLETOP -> {
                    top = SkinResources.getInstance(view.context).getDrawable(it.id)
                }
                DRAWABLERIGHT -> {
                    right = SkinResources.getInstance(view.context).getDrawable(it.id)
                }
                DRAWABLEBOTTOM -> {
                    bottom = SkinResources.getInstance(view.context).getDrawable(it.id)
                }

            }
            if (null != left || null != right || null != top || null != bottom) {
                (view as TextView).setCompoundDrawablesWithIntrinsicBounds(
                    left, top, right,
                    bottom
                )
            }
        }
    }
    private fun applySkinSupport() {
        Log.d("lib093", "SkinAttribute ===============applySkinSupport")
        if (view is SkinViewSupport) {
            (view as SkinViewSupport).applySkin()
        }
    }
}